package com.example.beikeyigou;

import android.annotation.SuppressLint;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;

import android.os.Handler;
import android.os.Message;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.beikeyigou.fpage.BannerAdapter;
import com.example.beikeyigou.fpage.FPTypeAdapter;
import com.example.beikeyigou.fpage.FPTypeVPAdapter;
import com.example.beikeyigou.fpage.HotFragment;
import com.example.beikeyigou.goods.TypeBean;
import com.example.beikeyigou.untils.BaseFragment;
import com.example.beikeyigou.untils.URLUtils;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;


public class FPageFragment extends BaseFragment {
    ViewPager typeVp,bannerVp;
    LinearLayout pointLayout;   //放置指示器小点点的布局
    List<ImageView>bannerList;
    List<ImageView>pointList;
    ImageView searchIv;
    EditText searchEt;


    int resId[] = {R.mipmap.ad1,R.mipmap.ad2}; //数据源的图片数组
    List<GridView> typeList;
    int pageSize = 8;
    int totalPage;

    //    定时器：定时滑动banner条
    Handler handler = new Handler(){
        @SuppressLint("HandlerLeak")
        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what == 1) {
                int currentItem = bannerVp.getCurrentItem();//获取当前滑动到哪一页了
                if (currentItem == bannerList.size()-1) {
                    bannerVp.setCurrentItem(0); //如果已经滑动到最后一页了，自动回到第一页
                }else{
                    currentItem++;
                    bannerVp.setCurrentItem(currentItem);
                }
                handler.sendEmptyMessageDelayed(1,3000);
            }
        }
    };
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_fpage, container, false);
        initView(view);
        loadFragment();
        setBannerPager();  //设置中间左右滑动的viewpager
        handler.sendEmptyMessageDelayed(1,3000);
        //setTypePager();
        getNetWork(URLUtils.queryAllCategory_url);
        return view;
    }

    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        TypeBean typeBean = new Gson().fromJson(result,TypeBean.class);
        List<TypeBean.DataBean> list = typeBean.getData();
        if (list!=null) {
            setTypePager(list);
        }

    }

    private void setTypePager(List<TypeBean.DataBean> list ) {
        typeList = new ArrayList<>();//存储viewpager的每一页

        int size = list.size();
        totalPage = (int)Math.ceil((size*1.0)/pageSize);
        for (int i = 0; i < totalPage; i++) {
            GridView gridView = new GridView(getContext());
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT);


            lp.setMargins(5,5,5,5);
            gridView.setLayoutParams(lp);
            gridView.setNumColumns(pageSize/2);
            //设置适配器
            FPTypeAdapter fpTypeAdapter = new FPTypeAdapter(getContext(),list,i,pageSize);
            gridView.setAdapter(fpTypeAdapter);
            typeList.add(gridView);


        }
        //设置viewpaper的适配器
        FPTypeVPAdapter adapter = new FPTypeVPAdapter(typeList);
        typeVp.setAdapter(adapter);


    }

    //设置中间左右滑动的viewpager
    private void setBannerPager() {
        bannerList = new ArrayList<>();//vp的数据源
        pointList = new ArrayList<>(); //小点点集合
        for (int i = 0; i < resId.length; i++) {
            ImageView iv = new ImageView(getContext());
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT);
            iv.setLayoutParams(params);
            iv.setImageResource(resId[i]);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            bannerList.add(iv);
            //指示器小点点
            ImageView piv = new ImageView(getContext());
            LinearLayout.LayoutParams p_params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            p_params.setMargins(0,0,8,0);//设置外边距
            piv.setLayoutParams(p_params);
            piv.setImageResource(R.mipmap.page__normal_indicator);
            pointList.add(piv);   //放倒集合当中统一管理
            pointLayout.addView(piv);   //加载到布局
        }
        //设置适配器
        BannerAdapter bannerAdapter = new BannerAdapter(bannerList);
        bannerVp.setAdapter(bannerAdapter);
        pointList.get(0).setImageResource(R.mipmap.page__selected_indicator);
        bannerVp.addOnPageChangeListener(pageChangeListener);
    }
    ViewPager.OnPageChangeListener pageChangeListener
            = new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            for (int i = 0; i < pointList.size(); i++) {
                pointList.get(i).setImageResource(R.mipmap.page__normal_indicator);
            }
            pointList.get(position).setImageResource(R.mipmap.page__selected_indicator);
        }
    };

    /* 将热销产品和产品推荐的fragment进行加载*/
    private void loadFragment() {
        FragmentManager fm = getChildFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.add(R.id.frag_fpage_layout_hot,
                HotFragment.newInstance(URLUtils.hotCommodity_url,""));
        transaction.add(R.id.frag_fpage_layout_recommend,
                HotFragment.newInstance(URLUtils.recommendCommodity_url,""));
        transaction.commit();
    }

    private void initView(View view) {
        typeVp = view.findViewById(R.id.frag_fpage_vp_type);
        bannerVp = view.findViewById(R.id.frag_fpage_vp_banner);
        pointLayout = view.findViewById(R.id.frag_fpage_layout_point);
        searchIv = view.findViewById(R.id.frag_search_iv);
        searchEt = view.findViewById(R.id.frag_search_et);

    }
}